package ru.niir.goodfil.utils;

import ru.niir.goodfil.db.Order;
import ru.niir.goodfil.db.OrderDeliveryInfoExt;
import ru.niir.goodfil.db.OrderItem;
import ru.niir.goodfil.db.dic.DeliveryType;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class ExportStringFormatter
{
    private final Order order;
    private final OrderDeliveryInfoExt deliveryInfo;
    private final List<OrderItem> orderItems;

    public ExportStringFormatter(Order order, List<OrderItem> orderItems)
    {
        if (order == null)
            throw new IllegalArgumentException("order is null");

        if (orderItems == null)
            throw new IllegalArgumentException("orderItems is null");

        this.order = order;
        this.deliveryInfo = order.getDeliveryInfoExt();
        this.orderItems = orderItems;
    }

    public String formatExportString()
    {
        java.sql.Date deliveryDate = order.getDeliveryDate();

        float weight = 0;
        for (ru.niir.goodfil.db.OrderItem oi : orderItems) {
            weight += (oi.getGood().getWeight() * oi.getCount());
        }

        float volume = 0;
        for (ru.niir.goodfil.db.OrderItem oi : orderItems) {
            volume += (oi.getGood().getVolume() * oi.getCount());
        }

        BigDecimal bdWeight = new BigDecimal(String.valueOf(weight)).setScale(1, RoundingMode.HALF_UP);
        BigDecimal bdVolume = new BigDecimal(String.valueOf(volume)).setScale(1, RoundingMode.HALF_UP);

        StringBuilder sb = new StringBuilder();
        sb.append(deliveryInfo.getPayTypeAsString() + ". ");

        if (order.getDeliveryTypeId() == DeliveryType.COMPANY)
            sb.append("Доставка автотранспортом компании. ");
        else if (order.getDeliveryTypeId() == DeliveryType.SELF_DELIVERY)
            sb.append("Самовывоз. ");
        else if(order.getDeliveryTypeId() == DeliveryType.DOCUMENS_EXECUTION){
            sb.append("ОФОРМЛЕНИЕ ДОКУМЕНТОВ");
        }

        if(order.getDeliveryTypeId() != DeliveryType.DOCUMENS_EXECUTION){
            if (deliveryDate != null)
            {
                sb.append("ДАТА ОТГРУЗКИ: ");
                SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
                java.util.Date d = new java.util.Date(deliveryDate.getTime());
                sb.append(sdf.format(d));
                sb.append(". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportName()))
            {
                sb.append("ТРАНСПОРТНАЯ КОМПАНИЯ: " + deliveryInfo.getTransportName() + ". ");
            }

            sb.append("ВЕС: " + bdWeight + ". ");
            sb.append("ОБЪЕМ: " + bdVolume + ". ");

            if (!StringUtils.isBlank(deliveryInfo.getTransportContactFio()))
            {
                sb.append("КОНТАКТ. ТК: " + deliveryInfo.getTransportContactFio() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportAddress()))
            {
                sb.append("АДРЕС ТК: " + deliveryInfo.getTransportAddress() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportTel()))
            {
                sb.append("ТЕЛ. ТК: " + deliveryInfo.getTransportTel() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetAddress()))
            {
                sb.append("АДРЕС ДОСТ.: " + deliveryInfo.getTargetAddress() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetContactFio()))
            {
                sb.append("КОНТАКТ. ДОСТ.: " + deliveryInfo.getTargetContactFio() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetTel()))
            {
                sb.append("ТЕЛ. ДОСТ.: " + deliveryInfo.getTargetTel() + ". ");
            }

        }

        if (!StringUtils.isBlank(deliveryInfo.getComment()))
        {
            sb.append(deliveryInfo.getComment());
        }

        return sb.toString();
    }

    public String formatExportStringforDeliveryList()
    {
        StringBuilder sb = new StringBuilder();
       // sb.append(deliveryInfo.getPayTypeAsString() + ". ");

        if (order.getDeliveryTypeId() == DeliveryType.COMPANY)
            sb.append("");
        else if (order.getDeliveryTypeId() == DeliveryType.DOCUMENS_EXECUTION) sb.append("ОФОРМЛЕНИЕ ДОКУМЕНТОВ");
        else if (order.getDeliveryTypeId() == DeliveryType.SELF_DELIVERY) sb.append("Самовывоз. ");
        else if (order.getDeliveryTypeId() == DeliveryType.TRANSPORT_COMPANY_RETURN) sb.append("ВОЗВРАТ ОТ ТК. ");

        if(order.getDeliveryTypeId() == DeliveryType.SELF_DELIVERY || order.getDeliveryTypeId() == DeliveryType.COMPANY){
            sb.append("Фирма: " + order.getFirmAsString() + " ");

            sb.append("Оплата: " + deliveryInfo.getPayTypeAsString() + ". ");
        }

        if(order.getDeliveryTypeId() == DeliveryType.COMPANY || order.getDeliveryTypeId() == DeliveryType.TRANSPORT_COMPANY_RETURN){

            if (!StringUtils.isBlank(deliveryInfo.getTransportName()))
            {
                sb.append("ТК: " + deliveryInfo.getTransportName() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportContactFio()))
            {
                sb.append("КОНТАКТ. ТК: " + deliveryInfo.getTransportContactFio() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportAddress()))
            {
                sb.append("АДРЕС ТК: " + deliveryInfo.getTransportAddress() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportTel()))
            {
                sb.append("ТЕЛ. ТК: " + deliveryInfo.getTransportTel() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetAddress()))
            {
                sb.append("АДРЕС ДОСТ.: " + deliveryInfo.getTargetAddress() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetContactFio()))
            {
                sb.append("КОНТАКТ. ДОСТ.: " + deliveryInfo.getTargetContactFio() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetTel()))
            {
                sb.append("ТЕЛ. ДОСТ.: " + deliveryInfo.getTargetTel() + ". ");
            }
        }

        if (!StringUtils.isBlank(deliveryInfo.getComment()))
        {
            sb.append(deliveryInfo.getComment());
        }

        return sb.toString();
    }

    public String formatExportStringForStorekeeper(){
        StringBuilder sb = new StringBuilder();
        if (!StringUtils.isBlank(deliveryInfo.getComment()))
        {
            sb.append(deliveryInfo.getComment());
        }
        return sb.toString();
    }

    public String formatShortExportString()
    {
        StringBuilder sb = new StringBuilder();
        // sb.append(deliveryInfo.getPayTypeAsString() + ". ");

        if (order.getDeliveryTypeId() == DeliveryType.COMPANY)
            sb.append("");
        else if (order.getDeliveryTypeId() == DeliveryType.SELF_DELIVERY)
            sb.append("Самовывоз. ");


        if(order.getDeliveryTypeId() == DeliveryType.COMPANY){

            if (!StringUtils.isBlank(deliveryInfo.getTransportName()))
            {
                sb.append("ТК: " + deliveryInfo.getTransportName() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportAddress()))
            {
                sb.append("АДРЕС ТК: " + deliveryInfo.getTransportAddress() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetAddress()))
            {
                sb.append("АДРЕС ДОСТ.: " + deliveryInfo.getTargetAddress() + ". ");
            }
        }

        return sb.toString();
    }

    public String formatExportStringforContactAndComment()
    {
        StringBuilder sb = new StringBuilder();
        // sb.append(deliveryInfo.getPayTypeAsString() + ". ");

            if (!StringUtils.isBlank(deliveryInfo.getTransportContactFio()))
            {
                sb.append("КОНТАКТ. ТК: " + deliveryInfo.getTransportContactFio() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTransportTel()))
            {
                sb.append("ТЕЛ. ТК: " + deliveryInfo.getTransportTel() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetContactFio()))
            {
                sb.append("КОНТАКТ. ДОСТ.: " + deliveryInfo.getTargetContactFio() + ". ");
            }

            if (!StringUtils.isBlank(deliveryInfo.getTargetTel()))
            {
                sb.append("ТЕЛ. ДОСТ.: " + deliveryInfo.getTargetTel() + ". ");
            }

        if (!StringUtils.isBlank(deliveryInfo.getComment()))
        {
            sb.append(deliveryInfo.getComment());
        }

        return sb.toString();
    }
}
